package cn.zust.ly.service.impl;

import cn.zust.ly.dao.hibernate.BaseDao;
import cn.zust.ly.dto.PicDto;
import cn.zust.ly.entity.Tconcern;
import cn.zust.ly.entity.Tpicture;
import cn.zust.ly.entity.Tuser;
import cn.zust.ly.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liy on 2016/12/5.
 */
@Service
@Transactional
public class PictureServiceImpl implements PictureService {

    @Autowired
    private BaseDao<Tpicture> pictureDao;

    @Autowired
    private BaseDao<Tuser> userDao;

    @Autowired
    private BaseDao<Tconcern> concernDao;


    @Override
    public boolean createPic(String fname, String tags, int userId) {
        if (fname == null || fname.length() <= 0) return false;
        else
            pictureDao.save(new Tpicture(fname, userDao.get(Tuser.class, userId), tags, new Date(), 0));
        return true;
    }

    @Override
    public boolean createPics(String[] pics, String tags, int userId) {
        if (pics[0] == null || pics[0].length() <= 0) return false;
        else {
            pictureDao.save(new Tpicture(pics[0], userDao.get(Tuser.class, userId), tags, new Date(), 0));
            if (pics[1].length()>0)
                pictureDao.save(new Tpicture(pics[1], userDao.get(Tuser.class, userId), tags, new Date(), 0));
            if (pics[2].length()>0)
                pictureDao.save(new Tpicture(pics[2], userDao.get(Tuser.class, userId), tags, new Date(), 0));

        }
           return true;
    }

    @Override
    public PicDto getUserOnlyPic(int userId) {
        Tpicture p = pictureDao.get("from Tpicture p where p.user.id =" + userId + " and p.intro = 'only'");
        Tuser user = userDao.get(Tuser.class, userId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (p == null) return null;
        else
            return new PicDto(p.getId(), p.getFname(), userId, user.getUsername(),
                p.getIntro(), p.getTags(), sdf.format(p.getUploadTime()));
    }


    @Override
    public List<PicDto> getUserPics(int userId) {
        List<Tpicture> tpictures = pictureDao.find("from Tpicture p where p.user.id =" + userId + " and p.intro = null  order by p.uploadTime desc");
        List<PicDto> picDtos = new ArrayList<>();
        if (tpictures.size()>0){
            Tuser user = userDao.get(Tuser.class, userId);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (Tpicture p :tpictures) {
                picDtos.add(new PicDto(p.getId(), p.getFname(), userId, user.getUsername(),
                        p.getIntro(), p.getTags(), sdf.format(p.getUploadTime())));
            }
        }

        return picDtos;
    }

    @Override
    public List<PicDto> getUserConcernerPics(int userId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<PicDto> picDtos = new ArrayList<>();
        List<Tuser> tusers = userDao.find("select c.concerned from Tconcern c where c.concerner.id = " + userId);
        System.out.println(tusers.size());
        if (tusers.size()>0) {
            for (Tuser u : tusers) {
                System.out.println(u.getName());
                List<Tpicture> tpictures = pictureDao.find("from Tpicture p where p.user.id =" + u.getId() + " and p.intro = null  order by p.uploadTime desc");
                if (tpictures.size()>0) {
                    Tuser user = userDao.get(Tuser.class, userId);
                    for (Tpicture p :tpictures) {
                        picDtos.add(new PicDto(p.getId(), p.getFname(), userId, user.getUsername(),
                                p.getIntro(), p.getTags(), sdf.format(p.getUploadTime())));
                    }
                }

            }
        }
        return picDtos;
    }
}
